package org.jeecg.modules.demo.expert.info.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.domain.types.entity.CmsDomainType;
import org.jeecg.modules.demo.domain.types.mapper.CmsDomainTypeMapper;
import org.jeecg.modules.demo.domain.types.service.ICmsDomainTypeService;
import org.jeecg.modules.demo.expert.info.entity.JkExpert;
import org.jeecg.modules.demo.expert.info.entity.JkExpertWork;
import org.jeecg.modules.demo.expert.info.entity.JkExpertAcademic;
import org.jeecg.modules.demo.expert.info.entity.JkExpertProfile;
import org.jeecg.modules.demo.expert.info.mapper.JkExpertWorkMapper;
import org.jeecg.modules.demo.expert.info.mapper.JkExpertAcademicMapper;
import org.jeecg.modules.demo.expert.info.mapper.JkExpertProfileMapper;
import org.jeecg.modules.demo.expert.info.mapper.JkExpertMapper;
import org.jeecg.modules.demo.expert.info.service.IJkExpertService;
import org.jeecg.modules.demo.expert.info.vo.JkExpertPage;
import org.jeecg.modules.demo.expert.info.vo.JkExpertPageVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.*;

/**
 * @Description: 专家信息表
 * @Author: jeecg-boot
 * @Date:   2025-06-07
 * @Version: V1.0
 */
@Service
public class JkExpertServiceImpl extends ServiceImpl<JkExpertMapper, JkExpert> implements IJkExpertService {

	@Autowired
	private JkExpertMapper jkExpertMapper;
	@Autowired
	private JkExpertWorkMapper jkExpertWorkMapper;
	@Autowired
	private JkExpertAcademicMapper jkExpertAcademicMapper;
	@Autowired
	private JkExpertProfileMapper jkExpertProfileMapper;
	@Autowired
	private CmsDomainTypeMapper cmsDomainTypeMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(JkExpert jkExpert, List<JkExpertWork> jkExpertWorkList,List<JkExpertAcademic> jkExpertAcademicList,List<JkExpertProfile> jkExpertProfileList) {
		jkExpertMapper.insert(jkExpert);
		if(jkExpertWorkList!=null && jkExpertWorkList.size()>0) {
			for(JkExpertWork entity:jkExpertWorkList) {
				//外键设置
				entity.setFid(jkExpert.getId());
				jkExpertWorkMapper.insert(entity);
			}
		}
		if(jkExpertAcademicList!=null && jkExpertAcademicList.size()>0) {
			for(JkExpertAcademic entity:jkExpertAcademicList) {
				//外键设置
				entity.setFid(jkExpert.getId());
				jkExpertAcademicMapper.insert(entity);
			}
		}
		if(jkExpertProfileList!=null && jkExpertProfileList.size()>0) {
			for(JkExpertProfile entity:jkExpertProfileList) {
				//外键设置
				entity.setFid(jkExpert.getId());
				jkExpertProfileMapper.insert(entity);
			}
		}
		//增加中间表
		jkExpertMapper.delete_expert_domain(jkExpert.getId());
		String[] files = jkExpert.getExpertiseField().split(",");
		if(files!=null && files.length>0) {
			for(String field_id:files) {
				jkExpertMapper.save_expert_domain(jkExpert.getId(),field_id);
			}
		}

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(JkExpert jkExpert,List<JkExpertWork> jkExpertWorkList,List<JkExpertAcademic> jkExpertAcademicList,List<JkExpertProfile> jkExpertProfileList) {
		jkExpertMapper.updateById(jkExpert);
		
		//1.先删除子表数据
		jkExpertWorkMapper.deleteByMainId(jkExpert.getId());
		jkExpertAcademicMapper.deleteByMainId(jkExpert.getId());
		jkExpertProfileMapper.deleteByMainId(jkExpert.getId());
		
		//2.子表数据重新插入
		if(jkExpertWorkList!=null && jkExpertWorkList.size()>0) {
			for(JkExpertWork entity:jkExpertWorkList) {
				//外键设置
				entity.setFid(jkExpert.getId());
				jkExpertWorkMapper.insert(entity);
			}
		}
		if(jkExpertAcademicList!=null && jkExpertAcademicList.size()>0) {
			for(JkExpertAcademic entity:jkExpertAcademicList) {
				//外键设置
				entity.setFid(jkExpert.getId());
				jkExpertAcademicMapper.insert(entity);
			}
		}
		if(jkExpertProfileList!=null && jkExpertProfileList.size()>0) {
			for(JkExpertProfile entity:jkExpertProfileList) {
				//外键设置
				entity.setFid(jkExpert.getId());
				jkExpertProfileMapper.insert(entity);
			}
		}
		//增加中间表
		jkExpertMapper.delete_expert_domain(jkExpert.getId());
		String[] files = jkExpert.getExpertiseField().split(",");
		if(files!=null && files.length>0) {
			for(String field_id:files) {
				jkExpertMapper.save_expert_domain(jkExpert.getId(),field_id);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		jkExpertWorkMapper.deleteByMainId(id);
		jkExpertAcademicMapper.deleteByMainId(id);
		jkExpertProfileMapper.deleteByMainId(id);
		jkExpertMapper.deleteById(id);
		jkExpertMapper.delete_expert_domain(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			jkExpertWorkMapper.deleteByMainId(id.toString());
			jkExpertAcademicMapper.deleteByMainId(id.toString());
			jkExpertProfileMapper.deleteByMainId(id.toString());
			jkExpertMapper.deleteById(id);
			jkExpertMapper.delete_expert_domain(id.toString());
		}
	}

	@Override
	public IPage<JkExpert> select_expert_by_code( String typeCode, Page<JkExpert> page) {
		IPage<JkExpert> pageLists =new Page<>();
		List list = new ArrayList<>();
		String id = null;
		CmsDomainType cmsDomainType = new CmsDomainType();
		cmsDomainType.setTypeCode(typeCode);
		List<CmsDomainType> tyeps=cmsDomainTypeMapper.get_doamin_type_by_code(cmsDomainType.getTypeCode());
		for (int i = 0; i < tyeps.size(); i++) {
			id=  tyeps.get(i).getId();
			QueryWrapper<JkExpert> query_expert = new QueryWrapper<>();
			query_expert.like("expertise_field",id);
			List<JkExpert> list1 = jkExpertMapper.selectList(page, query_expert);
			list.addAll(list1);
		}
		if(list!=null && list.size()>0){
//              将返回的专家list去重
			Set<JkExpert> set = new HashSet<>(list);
			List<JkExpert> uniqueList = new ArrayList<>(set);
			pageLists.setRecords(uniqueList);
		}
		return pageLists;
	}

	@Override

	public JkExpertPageVo query_jk_expert_details_by_id(String id) {
		JkExpertPageVo jkExpertPage = jkExpertMapper.query_jk_expert_details_by_id(id);
		return jkExpertPage;
	}

	@Override
	public List<Map<String, String>> query_expert_by_domaincode_top5(String typeCode) {
		return jkExpertMapper.query_expert_by_domaincode_top5(typeCode);
	}

	@Override
	public int query_expert_by_domaincode_count(String typeCode) {
		return jkExpertMapper.query_expert_by_domaincode_count(typeCode);

	}

}
